<template>
	<view>
		<navbar :autoBack='false'></navbar>
		<view class="con u-flex u-flex-column u-flex-items-center">
			<view class="logo">

			</view>
			<view class="title">
				欢迎来到贷款平台
			</view>
			<!-- 密码登录 -->
			<view class="form" v-if="ispwd">
				<view class="inpbox">
					<image src="/static/login/phone.png" class="inpbox__phone" mode=""></image>
					<input class="inpbox__inp" v-model="form.account" maxlength="11" type="number"
						placeholder="请输入账号" />
				</view>
				<view class="inpbox">
					<image src="/static/login/pws.png" class="inpbox__pws" mode=""></image>
					<input class="inpbox__inp" type="password" v-model="form.password" placeholder="请输入密码" />
				</view>
			</view>
			<!-- 验证码登录 -->
			<view class="form" v-else>
				<view class="inpbox">
					<image src="/static/login/phone.png" class="inpbox__phone" mode=""></image>
					<input class="inpbox__inp" type="number" maxlength="11" v-model="formcode.mobile" placeholder="请输入账号" />
				</view>
				<view class="inpbox">
					<image src="/static/login/yzm.png" class="inpbox__yzm" mode=""></image>
					<input class="inpbox__inp" type="password" placeholder="请输入验证码" v-model="formcode.captcha" />
					<u-button color="#3262FD" @tap="getCode"
						:customStyle='{width:"200rpx",height:"84rpx",borderRadius:"15rpx",marginRight:"10rpx",fontSize:"24rpx"}'>{{tips}}</u-button>
				</view>
			</view>
			<u-button text="登录" color="#3262FD" size="large" :loading="loading"
				:customStyle='{width:"606rpx",height:"111rpx",borderRadius:"15rpx",marginTop:"30rpx"}'
				@click="login"></u-button>
			<view class="tips">
				<view class="tips__item" v-if="ispwd" @click="ispwd=false">
					验证码登录
				</view>
				<view class="tips__item" v-else @click="ispwd=true">
					密码登录
				</view>
				<view class="tips__item" @click="navto('/pages/index/reg')">
					注册账号
				</view>
			</view>
			<view class="tips1">
				-- 微信登录 --
			</view>
			<image src="/static/login/wx.png" class="wx" mode=""></image>
		</view>
		<u-code :keep-running='true' change-text='Xs重新获取' :seconds="seconds" @end="end" @start="start" ref="uCode"
			@change="codeChange"></u-code>
	</view>
</template>

<script>
	import user from '@/api/_user.js'
	export default {

		data() {
			return {
				tips: '',
				// refCode: null,
				seconds: 60,
				ispwd: true,
				loading:false,
				form: {
					account: '',
					password: ''
				},
				formcode: {
					"mobile": "",
					"captcha": ""
				}
			};
		},
		onReady() {
			// 注意这里不能将一个组件赋值给data的一个变量，否则在微信小程序会
			// 造成循环引用而报错，如果你想这样做，请在非data中定义refCode变量
			this.refCode = this.$refs.uCode;
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			login() {
				let _this=this
				if(this.ispwd){
					if (!this.form.account) {
						this.$u.toast('请输入账号')
						return
					}
					if (!this.form.password) {
						this.$u.toast('请输入密码')
						return
					}
					this.loading=true
					user.login(this.form).then(res => {
						if (res.data.code == 1) {
							uni.$u.toast(res.data.msg)
							uni.setStorageSync('dktoken', res.data.data.userinfo.token)
							uni.setStorageSync('dkuserinfo', res.data.data.userinfo)
							setTimeout(() => {
								_this.loading=false
								uni.redirectTo({
									url: '/pages/index/index'
								})
							}, 1000)
						}else{
							_this.loading=false
						}
					})
				}else{
					if (!this.formcode.mobile) {
						this.$u.toast('请输入账号')
						return
					}
					if (!this.formcode.captcha) {
						this.$u.toast('请输入验证码')
						return
					}
					_this.loading=true
					user.mobilelogin(this.formcode).then(res=>{
						if (res.data.code == 1) {
							uni.$u.toast(res.data.msg)
							uni.setStorageSync('dktoken', res.data.data.userinfo.token)
							uni.setStorageSync('dkuserinfo', res.data.data.userinfo)
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/index/index'
								})
								_this.loading=false
							}, 1000)
						}else{
							_this.loading=false
						}
					})
				}
				

				// this.navto('/pages/index/index')
			},
			getCode() {
				if(!this.formcode.mobile){
					this.$u.toast('请输入账号')
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				uni.$u.toast('倒计时结束');
			},
			start() {
				uni.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style lang="scss">
	.con {
		padding-top: 100rpx;
		position: relative;
		padding-bottom: 150rpx;
	}

	.logo {
		width: 224rpx;
		height: 225rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 7rpx 40rpx 0rpx rgba(72, 139, 254, 0.18);
		border-radius: 90rpx;
	}

	.title {
		width: 326rpx;
		height: 39rpx;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 41rpx;
		color: #1E1E1E;
		line-height: 41rpx;
		margin: 70rpx 0 79rpx;
	}

	.inpbox {
		width: 606rpx;
		height: 107rpx;
		background: #F6F9FF;
		border-radius: 15rpx;
		display: flex;
		align-items: center;
		margin-bottom: 33rpx;

		&__pws {
			width: 32rpx;
			height: 37rpx;
			margin-left: 46rpx;
			margin-right: 28rpx;
		}

		&__phone {
			width: 28rpx;
			height: 35rpx;
			margin: 0 30rpx 0 46rpx;
		}

		&__yzm {
			width: 28rpx;
			height: 33rpx;
			margin: 0 30rpx 0 48rpx;
		}

		&__inp {
			flex: 1;
			font-family: PingFang SC;
			font-size: 28rpx;
			color: #333;
		}

		.uni-input-placeholder {
			color: #A1A4AA;
			font-family: PingFang SC;
			font-size: 28rpx;
		}
	}

	.tips {
		width: 606rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 26rpx;
		color: #A9ADB8;
		line-height: 26rpx;
		margin-top: 34rpx;
		margin-bottom: 100rpx;
	}

	.tips1 {
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #A1A4AA;
		line-height: 28rpx;
	}

	.wx {
		width: 112rpx;
		height: 112rpx;
		margin-top: 29rpx;
	}

	.u-button--normal {
		padding: 0;
	}
</style>